package com.ruoyi.dataBase.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.dataBase.domain.IndustrialTransportPlan;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.dataBase.domain.bo.IndustrialFunctDeptBo;
import com.ruoyi.dataBase.domain.vo.IndustrialFunctDeptVo;
import com.ruoyi.dataBase.domain.IndustrialFunctDept;
import com.ruoyi.dataBase.mapper.IndustrialFunctDeptMapper;
import com.ruoyi.dataBase.service.IIndustrialFunctDeptService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 职能部门Service业务层处理
 *
 * @author yangzhen
 * @date 2025-11-07
 */
@RequiredArgsConstructor
@Service
public class IndustrialFunctDeptServiceImpl implements IIndustrialFunctDeptService {

    private final IndustrialFunctDeptMapper baseMapper;

    /**
     * 查询职能部门
     */
    @Override
    public IndustrialFunctDeptVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询职能部门列表
     */
    @Override
    public TableDataInfo<IndustrialFunctDeptVo> queryPageList(IndustrialFunctDeptBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IndustrialFunctDept> lqw = buildQueryWrapper(bo);
        Page<IndustrialFunctDeptVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询职能部门列表
     */
    @Override
    public List<IndustrialFunctDeptVo> queryList(IndustrialFunctDeptBo bo) {
        LambdaQueryWrapper<IndustrialFunctDept> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IndustrialFunctDept> buildQueryWrapper(IndustrialFunctDeptBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IndustrialFunctDept> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFunctionalDeptName()), IndustrialFunctDept::getFunctionalDeptName, bo.getFunctionalDeptName());
        lqw.eq(StringUtils.isNotBlank(bo.getFunctionalDeptType()), IndustrialFunctDept::getFunctionalDeptType, bo.getFunctionalDeptType());
        lqw.like(StringUtils.isNotBlank(bo.getPhone()), IndustrialFunctDept::getPhone, bo.getPhone());
        lqw.like(StringUtils.isNotBlank(bo.getAddress()), IndustrialFunctDept::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), IndustrialFunctDept::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), IndustrialFunctDept::getLatitude, bo.getLatitude());
        lqw.eq(bo.getDataStatus() != null, IndustrialFunctDept::getDataStatus, bo.getDataStatus());

        lqw.like(StringUtils.isNotBlank(bo.getCreateBy()), IndustrialFunctDept::getCreateBy, bo.getCreateBy());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateBy()), IndustrialFunctDept::getUpdateBy, bo.getUpdateBy());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            IndustrialFunctDept::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            IndustrialFunctDept::getCreateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));

        return lqw;
    }

    /**
     * 新增职能部门
     */
    @Override
    public Boolean insertByBo(IndustrialFunctDeptBo bo) {
        IndustrialFunctDept add = BeanUtil.toBean(bo, IndustrialFunctDept.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改职能部门
     */
    @Override
    public Boolean updateByBo(IndustrialFunctDeptBo bo) {
        IndustrialFunctDept update = BeanUtil.toBean(bo, IndustrialFunctDept.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IndustrialFunctDept entity){
        //TODO 做一些数据校验,如唯一约束
        if (entity == null || StringUtils.isBlank(entity.getFunctionalDeptName())) {
            throw new IllegalArgumentException("职能部门名称不能为空");
        }

        LambdaQueryWrapper<IndustrialFunctDept> queryWrapper = new LambdaQueryWrapper<IndustrialFunctDept>()
            .eq(IndustrialFunctDept::getFunctionalDeptName, entity.getFunctionalDeptName());

        // 如果是更新操作，排除当前记录
        if (entity.getId() != null) {
            queryWrapper.ne(IndustrialFunctDept::getId, entity.getId());
        }

        long count = baseMapper.selectCount(queryWrapper);

        if (count > 0) {
            throw new RuntimeException("职能部门已存在");
        }
    }

    /**
     * 批量删除职能部门
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
